﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using eventr.View;

namespace eventr.View
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            
            this.Loaded += (s, e) =>
            {
                this.DataContext = new MainPageViewModel(SuperDeepZoomer) { DeepZoomSource = "../SampleDeepZoom/output.xml" };                
            };            
        }

        private void Slideshow_Next(object sender, EventArgs e)
        {
            SuperDeepZoomer.NextSlide();
        }

        private void Slideshow_Previous(object sender, EventArgs e)
        {
            SuperDeepZoomer.PrevSlide();
        }

        private void MainMenu_ZoomIn(object sender, EventArgs e)
        {
            SuperDeepZoomer.ZoomIn();
        }

        private void MainMenu_ZoomOut(object sender, EventArgs e)
        {
            SuperDeepZoomer.ZoomOut();
        }

        private void MainMenu_NavigateHome(object sender, EventArgs e)
        {
            SuperDeepZoomer.ShowAllImages();
        }

        private void MainMenu_ToggleFullScreen(object sender, EventArgs e)
        {
            Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;
        }

        private void MainMenu_ToggleSlideShow(object sender, EventArgs e)
        {
            if (SuperDeepZoomer.SlideshowRunning)
            {
                SuperDeepZoomer.StopSlideshow();
            }
            else
            {
                SuperDeepZoomer.StartSlideshow();
            }
        }

        bool viewCurrentFlyThrough = false;

        private void LayoutRoot_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.F)
            {
                if (!viewCurrentFlyThrough)
                {
                    SuperDeepZoomer.SwitchOverlaySource(SuperDeepZoom.FlyThroughSource.FromCurrentUser);

                    //this will break any binding that has been previously set for this property
                    //this is okay as the binding has performed it's duty by this time in *this*
                    //project, but just be wary of doing this.
                    SuperDeepZoomer.FlyThroughOverlayEnabled = true;
                }
                else
                {
                    SuperDeepZoomer.FlyThroughOverlayEnabled = false;
                }
                viewCurrentFlyThrough = !viewCurrentFlyThrough;
            }
            else if (e.Key == Key.P)
            {
                if (SuperDeepZoomer.SlideshowRunning)
                {
                    SuperDeepZoomer.StopSlideshow();
                }
                else
                {
                    SuperDeepZoomer.PlayFlyThrough(TimeSpan.FromSeconds(2));
                }
            }
            else if (e.Key == Key.G)
            {
                ViewModel.Commands["ToggleMostViewed"].Execute();
            }
        }

        private void FlyThroughInfoPopup_PlayFlyThrough(object sender, EventArgs e)
        {
            SuperDeepZoomer.HideFlyThroughOverlay();
            SuperDeepZoomer.PlayFlyThrough(TimeSpan.FromSeconds(5));
        }

        private void FlyThroughPopup_HideFlyThrough(object sender, EventArgs e)
        {
            SuperDeepZoomer.HideFlyThroughOverlay();
        }

    }
}
